package vn.conghuy.talkwithme.data.sqlmodel;

import java.util.ArrayList;

import vn.conghuy.talkwithme.android.utils.DatabaseHelper;
import android.database.Cursor;

public class TemplatePair {
	/**
	 * @uml.property  name="_id"
	 */
	public int _id;
	/**
	 * @uml.property  name="askId"
	 */
	public int askId;
	/**
	 * @uml.property  name="answerId"
	 */
	public int answerId;
	
	/**
	 * @uml.property  name="exAsk"
	 */
	public String exAsk;
	/**
	 * @uml.property  name="exAnswer"
	 */
	public String exAnswer;
	
	public TemplatePair(){}
	
	public TemplatePair(int _id, int askId, int answerId) {
		super();
		this._id = _id;
		this.askId = askId;
		this.answerId = answerId;
	}

	public void receiveData(int _id){
		this._id = _id;
		String query = String.format("select * from TemplatePair where _id = %d ", _id);
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		if(cursor.moveToFirst()){
			askId = cursor.getInt(cursor.getColumnIndex("askId"));
			answerId = cursor.getInt(cursor.getColumnIndex("answerId"));
			
			WordModel word = new WordModel();
			word.receiveData(askId);
			exAsk = word.name;
			
			word.receiveData(answerId);
			exAnswer = word.name;
		}
		cursor.close();
	}
	
	public static String[] getAskList(){
		String[] askNames = null;
		String query = String.format("select distinct Word.name from TemplatePair, Word where TemplatePair.askId = Word._id");
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		if(cursor.moveToFirst()){
			askNames = new String[cursor.getCount()];
			int i = 0;
			do{
				String name = cursor.getString(cursor.getColumnIndex("name"));
				askNames[i] = name;
				i++;
			}while(cursor.moveToNext());
			
		}
		cursor.close();
		return askNames;
	}
	/**
	 * Find template has parameter, mark by "<", ">" pair
	 * @return list string of sentence has parameter mark
	 */
	public static String[] getParamAskList(){
		ArrayList<String> askNames = new ArrayList<String>();
		String query = String.format("select distinct Word.name from TemplatePair, Word where TemplatePair.askId = Word._id");
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		if(cursor.moveToFirst()){
			do{
				String name = cursor.getString(cursor.getColumnIndex("name"));
				if(name.contains("<") && name.contains(">"))
					askNames.add(name);
			}while(cursor.moveToNext());
			
		}
		
		//convert to string array
		String[] arStr = new String[askNames.size()];
		for(int i = 0; i < askNames.size(); i++){
			arStr[i] = askNames.get(i);
		}
		cursor.close();
		return arStr;
	}
	
	public static String[] getAnswerByAskName(String ask){
		String[] answerNames = null;
		ask = ask.replace("'", "''");
		ask = ask.replace("\"", "\"\"");
		String query = String.format("select TemplatePair._id from TemplatePair, Word where " +
				"TemplatePair.askId = Word._id and Word.name = '%s'", ask);
		Cursor cursor = DatabaseHelper.GetInstance().ExecuteQuery(query);
		if(cursor.moveToFirst()){
			answerNames = new String[cursor.getCount()];
			int i = 0;
			do{
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				TemplatePair templatePair = new TemplatePair();
				templatePair.receiveData(id);
				answerNames[i] = templatePair.exAnswer;
				i++;
			}while(cursor.moveToNext());
			
		}
		cursor.close();
		return answerNames;
	}
}
